<?php

class OrderController extends Uuch_BaseController{
    
    public function init() {
        parent::init();
        $this->_helper->layout->setLayout('admin');
    }
    
    public function indexAction(){
		   //中文
    }
	
	public function queryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('member') != '') {
			$where .= $db->quoteInto(' AND userId = ?', $this->_getParam('member'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND status = ?', $this->_getParam('state'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_order WHERE $where");
		$rows = $db->fetchAll("SELECT * FROM uuch_order WHERE $where LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function pagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('member2') != '') {
			$where .= $db->quoteInto(' AND userId = ?', $this->_getParam('member'));
		}
		if ($this->_getParam('state2') != '') {
			$where .= $db->quoteInto(' AND status = ?', $this->_getParam('state'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT * FROM uuch_order WHERE $where LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function detailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT * FROM uuch_order WHERE id = :id",
			array('id' => $this->_getParam('orderId'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function editAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$set = array (
			'status' => $this->_getParam('orderState'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('orderId'));
		$rows_affected = $db->update('uuch_order', $set, $where);
		echo json_encode(true);
		exit;
	}
	
	public function delAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_order', $where);
		echo json_encode(true);
		exit;
	}
	
	public function warehouseAction() 
	{
		
	}
	
	public function warehouseQueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('productName') != '') {
			$where .= $db->quoteInto(' AND productName = ?', $this->_getParam('productName'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND state = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND createdate > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND createdate < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_storeitem s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where");
		$rows = $db->fetchAll("SELECT s.*,u.username FROM uuch_storeitem s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function warehousePagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('productName') != '') {
			$where .= $db->quoteInto(' AND productName = ?', $this->_getParam('productName'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND state = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND createdate > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND createdate < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT s.*,u.username FROM uuch_storeitem s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function warehouseDetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT * FROM uuch_storeitem WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function warehouseEditAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');		
		$set = array (
			'productName' => $this->_getParam('productName'),
			'state' => $this->_getParam('state'),
			'storeId' => $this->_getParam('storeId'),
			'intype' => $this->_getParam('intype'),
			'createdate' => $this->_getParam('createdate'),
			'price' => $this->_getParam('price'),
			'quantity' => $this->_getParam('quantity'),
			'weight' => $this->_getParam('weight'),
			'dateInStore' => $this->_getParam('dateInStore'),
			'remark' => $this->_getParam('remark'),
			'orderNo' => $this->_getParam('orderNo'),
			'productId' => $this->_getParam('productId'),
			'logistics' => $this->_getParam('logistics'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_storeitem', $set, $where);
		echo json_encode(true);
		exit;
	}
	
	public function warehouseDelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_storeitem', $where);
		echo json_encode(true);
		exit;
	}
	
	public function transportAction() 
	{
		
	}
	
	public function transportQueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('productName') != '') {
			//$where .= $db->quoteInto(' AND productName = ?', $this->_getParam('productName'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND u.username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND w.status = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND w.dateCreated > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND w.dateCreated < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_waybill w LEFT JOIN uuch_user u ON w.userId = u.id WHERE $where");
		$rows = $db->fetchAll("SELECT w.*,u.username FROM uuch_waybill w LEFT JOIN uuch_user u ON w.userId = u.id WHERE $where LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function transportPagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('productName') != '') {
			//$where .= $db->quoteInto(' AND productName = ?', $this->_getParam('productName'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND u.username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND w.status = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND w.dateCreated > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND w.dateCreated < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT w.*,u.username FROM uuch_waybill w LEFT JOIN uuch_user u ON w.userId = u.id WHERE $where LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function transportDetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT * FROM uuch_waybill WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function transportEditAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');	
		$set = array (
			//'productName' => $this->_getParam('productName'),
			'shipType' => $this->_getParam('shipType'),
			'logistics' => $this->_getParam('logistics'),
			'weight' => $this->_getParam('weight'),
			'bagWeight' => $this->_getParam('bagWeight'),
			'discountFee' => $this->_getParam('discountFee'),
			'shipFee' => $this->_getParam('shipFee'),
			'serviceFee' => $this->_getParam('serviceFee'),
			'shipMoney' => $this->_getParam('shipMoney'),
			'status' => $this->_getParam('status'),
			'consignee' => $this->_getParam('consignee'),
			'conTel' => $this->_getParam('conTel'),
			'conPostCode' => $this->_getParam('conPostCode'),
			'conAdd' => $this->_getParam('conAdd'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_waybill', $set, $where);
		echo json_encode(true);
		exit;
	}
	
	public function transportDelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_waybill', $where);
		echo json_encode(true);
		exit;
	}
	
	public function transportLogAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = $db->quoteInto(' logisticsNo = ?', $this->_getParam('id'));
		$rows = $db->fetchAll("SELECT l.*,u.username FROM uuch_logisticslog l LEFT JOIN uuch_user u ON l.opUser = u.id WHERE $where");
		echo json_encode($rows);
		exit;
	}
	
	public function transportAddLogAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		
		//"logisticsNo="+logisticsNo_value+"opDate="+opDate_value+"opState="+opState_value+"opRemark="+opRemark_value
		$row = array (
			'logisticsNo' => $this->_getParam('logisticsNo'),
			'opDate' => $this->_getParam('opDate'),
			'opState' => $this->_getParam('opState'),
			'opRemark' => $this->_getParam('opRemark'),
			'opUser' => 1,
		);
		$rows_affected = $db->insert('uuch_logisticslog', $row);
		$last_insert_id = $db->lastInsertId();
		echo $last_insert_id;
		exit;
	}
	
	public function transportEditLogAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$set = array (
			'opDate' => $this->_getParam('opDate'),
			'opState' => $this->_getParam('opState'),
			'opRemark' => $this->_getParam('opRemark'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_logisticslog', $set, $where);
		echo 1;
		exit;
	}
	
	public function ingstoreAction() 
	{
		
	}
	
	public function ingstoreQueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('logisticsNo') != '') {
			$where .= $db->quoteInto(' AND logistics = ?', $this->_getParam('logisticsNo'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND state = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND createdate > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND createdate < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_ingstore s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where");
		$rows = $db->fetchAll("SELECT s.*,u.username FROM uuch_ingstore s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
	
	public function ingstorePagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('logisticsNo') != '') {
			$where .= $db->quoteInto(' AND logistics = ?', $this->_getParam('logisticsNo'));
		}
		if ($this->_getParam('username') != '') {
			$where .= $db->quoteInto(' AND username = ?', $this->_getParam('username'));
		}
		if ($this->_getParam('state') != '') {
			$where .= $db->quoteInto(' AND state = ?', $this->_getParam('state'));
		}
		if ($this->_getParam('starttime') != '') {
			$where .= $db->quoteInto(' AND createdate > ?', $this->_getParam('starttime'));
		}
		if ($this->_getParam('endtime') != '') {
			$where .= $db->quoteInto(' AND createdate < ?', $this->_getParam('endtime'));
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT s.*,u.username FROM uuch_ingstore s LEFT JOIN uuch_user u ON s.userid = u.id WHERE $where LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function ingstoreDelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_ingstore', $where);
		echo json_encode(true);
		exit;
	}
	
	public function ingstoreDetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT * FROM uuch_ingstore WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function ingstoreEditAction() {
		//$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
//		$db = $bootstrap->getResource('db');		
//		$set = array (
//			'productName' => $this->_getParam('productName'),
//			'state' => $this->_getParam('state'),
//			'storeId' => $this->_getParam('storeId'),
//			'intype' => $this->_getParam('intype'),
//			'createdate' => $this->_getParam('createdate'),
//			'price' => $this->_getParam('price'),
//			'quantity' => $this->_getParam('quantity'),
//			'weight' => $this->_getParam('weight'),
//			'dateInStore' => $this->_getParam('dateInStore'),
//			'remark' => $this->_getParam('remark'),
//			'orderNo' => $this->_getParam('orderNo'),
//			'productId' => $this->_getParam('productId'),
//			'logistics' => $this->_getParam('logistics'),
//		);
//		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
//		$rows_affected = $db->update('uuch_storeitem', $set, $where);
		echo json_encode(true);
		exit;
	}
	
	public function ingstoreFileAction() {
		$file=current($_FILES);		
		$upFilePath = APPLICATION_PATH . "/data/" . iconv('UTF-8','GBK',$file['name']);
        $ok=@move_uploaded_file($file['tmp_name'],$upFilePath);
	    echo json_encode($ok);
		exit;
	}
    
    public function orderAction(){
        $this->view->orders = Application_Model_Order::getByFilter(NULL,NULL,NULL,10,0);
    }
    
}